package com.hfjyz.system.category.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Transactional;

import com.hfjyz.base.bean.SearchCondition;
import com.hfjyz.base.constant.BaseConstant;
import com.hfjyz.base.entity.CategoryEntity;
import com.hfjyz.base.exception.BaseException;
import com.hfjyz.system.category.bean.CategoryBean;
import com.hfjyz.system.category.dao.CategoryDao;

public class CategoryService {
    
    private static final Log SYS_LOG = LogFactory.getLog(BaseConstant.SYS_LOG);

    private CategoryDao dao = null;

    public CategoryDao getDao() {
        return dao;
    }

    public void setDao(CategoryDao dao) {
        this.dao = dao;
    }

    public List<CategoryBean> findCategory(SearchCondition cond, Integer parentId) throws BaseException {
        try {
            List<CategoryEntity> categoryList = null;
            if (parentId == null) {
                categoryList = dao.findCategory(cond);
            } else {
                categoryList = dao.findCategory(cond, parentId);
            }

            List<CategoryBean> beanList = new ArrayList<CategoryBean>();
            for (CategoryEntity entity : categoryList) {
                CategoryBean bean = new CategoryBean(entity);
                beanList.add(bean);
            }

            SYS_LOG.info("检索分类成功。");
            return beanList;
        } catch (Exception e) {
            SYS_LOG.error("检索分类失败！", e);
            throw new BaseException("检索分类失败！", e);
        }
    }
    
    public List<CategoryBean> findCategory(Integer parentId) throws BaseException {
        try {
            List<CategoryEntity> categoryList = dao.findCategory(parentId);
            List<CategoryBean> beanList = new ArrayList<CategoryBean>();
            for (CategoryEntity entity : categoryList) {
                CategoryBean bean = new CategoryBean(entity);
                beanList.add(bean);
            }

            SYS_LOG.info("检索分类成功。");
            return beanList;
        } catch (Exception e) {
            SYS_LOG.error("检索分类失败！", e);
            throw new BaseException("检索分类失败！", e);
        }
    }

    public long getCategoryCount(SearchCondition cond, Integer parentId) throws BaseException {
        try {
            long count = 0L;
            if (parentId == null) {
                count = dao.countCategory(cond);
            } else {
                count = dao.countCategory(cond, parentId);
            }
            SYS_LOG.info("获取分类信息数量成功。 count=" + count);
            
            return count;
        } catch (Exception e) {
            SYS_LOG.error("获取分类信息数量失败！", e);
            throw new BaseException("获取分类信息数量失败！", e);
        }
    }

    @Transactional
    public void saveCategory(CategoryBean bean) throws BaseException {
        try {
            String msg = bean.validate();
            if (msg != null && !msg.isEmpty()) {
                throw new BaseException(msg);
            }
            CategoryEntity entity = null;
            if (bean.getId() == null) {
                long count = dao.countCategoryByName(bean.getName());
                if (count > 0) {
                    throw new BaseException("该分类已经存在！");
                }
                entity = new CategoryEntity();
            } else {
                entity = dao.get(bean.getId());
                if (!bean.getName().equals(entity.getCategoryName())) {
                    long count = dao.countCategoryByName(bean.getName());
                    if (count > 0) {
                        throw new BaseException("该分类已经存在！");
                    }
                }
            }
            entity.setId(bean.getId());
            entity.setCategoryName(bean.getName());
            entity.setCategoryRemark(bean.getRemark());
            entity.setCategoryParent(bean.getParent());

            dao.saveOrUpdate(entity);
            
            SYS_LOG.info("保存分类成功。 category=" + bean.toString());
        } catch (BaseException e) {
            SYS_LOG.error("保存分类成功失败！", e);
            throw e;
        } catch (Exception e) {
            SYS_LOG.error("保存分类成功失败", e);
            throw new BaseException("保存分类成功失败！", e);
        }
    }

    @Transactional
    public void deleteCategory(List<Integer> idList) throws BaseException {
        try {
            for (Integer id : idList) {
                dao.delete(id);
            }
            SYS_LOG.info("删除分类成功。 idList=" + idList.toString());
        } catch (Exception e) {
            SYS_LOG.error("删除分类成功失败！", e);
            throw new BaseException("删除分类成功失败！", e);
        }
    }

    public CategoryBean updateInitCategory(Integer id) throws BaseException {
        try {
            CategoryEntity entity = dao.get(id);
            CategoryBean bean = new CategoryBean(entity);
            SYS_LOG.info("获取分类信息成功。 bean=" + bean);

            return bean;
        } catch (Exception e) {
            SYS_LOG.error("获取分类信息失败！", e);
            throw new BaseException("获取分类信息失败！", e);
        }
    }
}
